package org.ktx.osr.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import org.ktx.osr.MainActivity
import org.ktx.osr.R

/**
 * 录制通知管理器
 * 负责管理录制过程中的通知显示
 * 功能边界：仅处理通知相关逻辑，不涉及录制功能
 */
class RecordingNotificationManager(private val context: Context) {
    private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    private val notificationId = 123
    private val notificationChannelId = "ScreenRecordChannel"

    init {
        createNotificationChannel()
    }

    /**
     * 创建前台服务通知
     */
    fun createForegroundNotification(): Notification {
        val notificationIntent = Intent(context, MainActivity::class.java)
        val pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        val contentPendingIntent = PendingIntent.getActivity(
            context, 0, notificationIntent, pendingIntentFlags
        )

        return NotificationCompat.Builder(context, notificationChannelId)
            .setContentTitle("Screen Recording")
            .setContentText("Recording in progress...")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(contentPendingIntent)
            .setOngoing(true)
            .build()
    }

    /**
     * 检查是否为前台服务
     */
    fun isForegroundService(): Boolean {
        val activeNotifications = notificationManager.activeNotifications
        for (notification in activeNotifications) {
            if (notification.id == notificationId) {
                return true
            }
        }
        return false
    }

    /**
     * 创建通知渠道
     */
    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val serviceChannel = NotificationChannel(
                notificationChannelId,
                "Screen Recording Service Channel",
                NotificationManager.IMPORTANCE_LOW
            )
            notificationManager.createNotificationChannel(serviceChannel)
        }
    }
}
